package com.tian.plugtool.strokesort.util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import com.tian.plugtool.strokesort.model.Stroke;
import com.tian.plugtool.strokesort.model.StrokeMap;

public class MapperObject {
    public StrokeMap getStrokeMapper() {
        StrokeMap mapper = null;
        ObjectInputStream is = null;
        try {
            //String path = SpringContextUtil.getBean("GetSoftWarePath").toString();
            String path =  System.getProperty("user.dir");
            //String path = MapperObject.class.getClassLoader().getResource("datfiles").getPath() + "/ChinessStroke.dat";
            mapper = setStrokeMap(path + "/warepath/ChinessStroke.dat");

            //mapper = dat2Object("D:/qdrd/qdnrddb/rdxt-common/target/classes/datfiles/ChinessStroke.dat", StrokeMap.class);
            //mapper = (StrokeMap) is.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null)
                    is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return mapper;
    }

    public static void object2Dat(Object object, String fileName)
            throws FileNotFoundException, IOException, Exception {
        // 创建输出文件
        File fo = new File(fileName);
        // 文件不存在,就创建该文件
        if (!fo.exists()) {
            // 先创建文件的目录
            String path = fileName.substring(0, fileName.lastIndexOf('.'));
            File pFile = new File(path);
            pFile.mkdirs();
        }
        FileOutputStream fs = new FileOutputStream(fileName);
        ObjectOutputStream oo = new ObjectOutputStream(fs);
        oo.writeObject(object);
        oo.flush();
        oo.close();
        fs.close();

    }

    public static <T> T dat2Object(String objSource, Class<T> class1) {
        T object = null;
        FileInputStream fi = null;
        ObjectInputStream oi = null;
        try {
            fi = new FileInputStream(objSource);
            oi = new ObjectInputStream(fi);
            object = (T) oi.readObject();
        } catch (Exception e) {
            e.printStackTrace();
            object = null;
        } finally {
            try {
                oi.close();
                fi.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return object;
    }

    //配置数据
    public StrokeMap setStrokeMap(String filePath) throws IOException {
        StrokeMap mapper = new StrokeMap();
        //File file = new File(filePath);
        //InputStream is=this.getClass().getResourceAsStream("/resource/res.txt");
        //BufferedReader br=new BufferedReader(new InputStreamReader(is,"utf-8"));
        FileInputStream fis = new FileInputStream(filePath);
        DataInputStream dis = new DataInputStream(fis);
        BufferedReader reader = new BufferedReader(new InputStreamReader(dis, "utf-8"));
        String lineStr;

        while (null != (lineStr = reader.readLine())) {
            if (!lineStr.trim().equals("")) {
                Stroke stroke = new Stroke();
                String[] data = lineStr.split("\\|");//需转义
/*                if (data.length != 4) {
                    System.out.println(lineStr);
                    System.out.println(data[0]);
                }*/
                int order = Integer.parseInt(data[0]);
                String name = data[1];
                int count = Integer.parseInt(data[2]);
                int code = Integer.parseInt(data[3]);
                stroke.setOrder(order);
                stroke.setCount(count);
                stroke.setCode(code);
                stroke.setName(name);
                mapper.put(stroke);
            }
        }
        reader.close();
        return mapper;
    }
}
